﻿using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Sharp.Portal.Configuration;
// ReSharper disable Mvc.ViewNotResolved

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    [AutomaticTemplate]
    public class UserController : SharpController
    {
        public ActionResult Index()
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var model = new UserIndexViewModel
                {
                    Roles = proxy.Channel.GetRoles()
                };
                return View(model);
            }
        }

        /// <summary>
        /// 用户分页数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ActionResult GetPaged(DataTableParameter param)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {

                var page = proxy.Channel.GetUsersPaged(param.ToPagingFeature());

                return this.Store(page);
            }
        }

        public ActionResult Details(int? id)
        {
            UserViewModel model;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var user = proxy.Channel.GetUserByKey(id.Value).Data;
                model = user.To<UserViewModel>();
                model.Password = "";
            }

            return View(model);
        }

        [Permission("新增", 1, "001001001")]
        public ActionResult Create()
        {
            var model = new UserViewModel();


            ViewBag.EditUserNameAble = true; // 是否可以编辑用户名
            return View(model);
        }


        [Permission("编辑", 2, "001001002")]
        public ActionResult Edit(int id)
        {
            UserViewModel model;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var user = proxy.Channel.GetUserByKey(id).Data;
                model = user.To<UserViewModel>();
                model.Password = "";
            }

            ViewBag.EditUserNameAble = false; // 是否可以编辑用户名
            return View(model);
        }


        public ActionResult Save(UserDTO vm)
        {
            if (vm == null)
            {
                return this.JsonNet(new Result() { Err = "提交数据出错。" });
            }

            foreach (string file in Request.Files)
            {
                HttpPostedFileBase uploadFile = Request.Files[file];

                if (uploadFile != null && uploadFile.ContentLength > 0)
                {
                    var stream = uploadFile.InputStream;

                    byte[] inputBytes = new byte[stream.Length];
                    stream.Read(inputBytes, 0, inputBytes.Length);
                    stream.Seek(0, SeekOrigin.Begin);

                    vm.Face = $"data:{uploadFile.ContentType};base64," + Convert.ToBase64String(inputBytes);

                    break;
                }

            }


            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {

                var res = proxy.Channel.SaveUser(vm);

                return this.JsonNet(res);
            }
        }

        [Permission("删除", 3, "001001003")]
        public ActionResult Delete(int[] ids)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var res = proxy.Channel.DeleteUsers(ids);
                return this.JsonNet(res);
            }
        }

        public ActionResult Config()
        {
            var vm = new UserConfigViewModel();
            vm.PageSize = UserConfigManage.Read<UserConfigInfo>().PageSize;

            vm.UI.PostAction = "SaveConfig";
            return View(vm);
        }

        public ActionResult SaveConfig(UserConfigInfo vm)
        {
            UserConfigManage.Save(vm);

            return this.JsonNet(new Result());
        }

        public ActionResult UserDialog(string selectTextControl)
        {
            ViewBag.SelectTextControl = selectTextControl;
            return View("UserDialog");
        }
    }
}